<?php


namespace App\Repositories\Settings;


use App\Models\BaseModel;
use App\Models\Settings\CodeValue;
use App\Repositories\BaseRepository;
use App\Utils\Output;
use App\Utils\Pager;

class CodeValueRepository extends BaseRepository
{
    function adminPager($params)
    {
        extract($params);
        $query = CodeValue::query();
        if (!empty($code)) {
            $query->where('code', '=', $code);
        }
        if (!empty($keyword)) {
            $query->where(function ($q) use ($keyword) {
                $q->orWhere('desc', 'like', "%$keyword%");
                $q->orWhere('value', 'like', "%$keyword%");
                $q->orWhere('name', 'like', "%$keyword%");
            });
        }
        $total = $query->count();
        $lis = Pager::setPageParams($query, $params)->get();
        return Output::pager($lis, $total);
    }

    function value($code)
    {
        $max = CodeValue::query()->where('code', '=', $code)->max('value');
        return (empty($max) ? 0 : $max) + 1;
    }

    function editInfo($id)
    {
        $entity = CodeValue::find($id);
        if ($entity) {
            return Output::data($entity);
        }
        return Output::fail("数据不存在");
    }


    /**
     * 删除
     * @param $id
     * @return Output|int
     */
    function destroy($id)
    {
        return CodeValue::destroy($id);
    }

    function getModel(): BaseModel
    {
        return new CodeValue();
    }
}
